﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyServer.Common
{
    /// <summary>
    /// 表示一个连接管理器基类
    /// </summary>
    public abstract class ConnectManagerBase
    {

        #region==========事件==========

        /// <summary>
        /// 当新连接加入时执行的事件
        /// </summary>
        public event NewConnectionJoinEventHandler NewConnectionJoin;
        /// <summary>
        /// 当连接关闭时执行的事件
        /// </summary>
        public event ConnectionCloseEventHandler ConnectionClose;

        #endregion

        #region========公有委托========

        /// <summary>
        /// 对新连接加入事件提供委托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public delegate void NewConnectionJoinEventHandler(object sender,ref ConnectEventArgs e);
        /// <summary>
        /// 为连接关闭事件提供委托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public delegate void ConnectionCloseEventHandler(object sender,ref ConnectEventArgs e);

        #endregion

        #region======继承事件委托======
        protected virtual void OnNewConnectionJoin(object sender,ref ConnectEventArgs e)
        {
            if(NewConnectionJoin!=null)
                NewConnectionJoin(sender,ref e);
        }
        protected virtual void OnConnectionClose(object sender,ref ConnectEventArgs e)
        {
            if(ConnectionClose!=null)
                ConnectionClose(sender,ref e);
        }
        #endregion

        #region==========属性=========

        /// <summary>
        /// 表示当前连接管理器所包含的连接列表
        /// </summary>
        public List<object> ClientList { get; private set; }
        /// <summary>
        /// 表示当前连接管理器所连接的收发中心
        /// </summary>
        public DataCenterBase Center { get; private set; }
        #endregion

        #region========抽象函数========

        /// <summary>
        /// 开始侦听新连接加入
        /// </summary>
        public abstract void StartListen();
        /// <summary>
        /// 停止连接管理器
        /// </summary>
        public abstract void Stop();

        #endregion

        #region========构造函数========

        /// <summary>
        /// 通过指定收发中心构造连接中心实例
        /// </summary>
        /// <param name="Center">要绑定的收发中心</param>
        public ConnectManagerBase(DataCenterBase Center)
        {
            this.Center = Center;
        }

        #endregion

    }
}
